package com.quanyan.place.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import com.quanyan.common.utils.DateTimeUtils;
import com.quanyan.place.constants.PlaceConstants;
import com.quanyan.place.entity.apiresp.RespPlaceUnitSalePlan;
import com.quanyan.place.entity.apiresp.RespPlaceUnitSalePlanVo;

/**
 * Created by songfan on 2017/3/31.
 */
public class PlaceOrderUtil {

	 /**
     * 根据起始时间与终止时间以及每小时间隔得到时段列表
     *
     * @param start
     * @param end
     * @param interval
     * @return
     */
    public static List<String> getIntervalTimeList(String start, String end, int interval) {
        Date startDate = DateTimeUtils.convertString2Date("HH:mm", start);
        Date endDate = DateTimeUtils.convertString2Date("HH:mm", end);
        List<String> list = new ArrayList<>();
        while (startDate!=null && endDate!=null && startDate.getTime() <= endDate.getTime()) {
            list.add(DateTimeUtils.convertDate2String("HH:mm", startDate));
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startDate);
            calendar.add(Calendar.MINUTE, interval);
            if (calendar.getTime().getTime() > endDate.getTime()) {
                if (!startDate.equals(endDate)) {
                    list.add(DateTimeUtils.convertDate2String("HH:mm", endDate));
                }
                startDate = calendar.getTime();
            } else {
                startDate = calendar.getTime();
            }

        }
        return list;
    }
    
   
    /**
     * 在场馆营业时间范围内，填充该时段缺少的销售计划
     *
     * @param list
     * @param dbPlaceUnitSalePlanVoList
     * @return
     */
    public static List<RespPlaceUnitSalePlanVo> getAllRespPlaceUnitSalePlanVoList(List<String> list,
                                                                                  List<RespPlaceUnitSalePlanVo> dbPlaceUnitSalePlanVoList) {
        //根据场馆营业时间得到原始的场馆销售计划
        List<RespPlaceUnitSalePlanVo> respPlaceUnitSalePlanVoList = new ArrayList<>();
        for (int i = 0; i < list.size() - 1; i++) {
            RespPlaceUnitSalePlan respPlaceUnitSalePlan = new RespPlaceUnitSalePlan();
            respPlaceUnitSalePlan.setStartTime(list.get(i));
            respPlaceUnitSalePlan.setEndTime(list.get(i + 1));
            respPlaceUnitSalePlan.setStatus(PlaceConstants.PLACE_ORDER_STATUS);
            respPlaceUnitSalePlan.setBundleNo(0);
            RespPlaceUnitSalePlanVo respPlaceUnitSalePlanVo = new RespPlaceUnitSalePlanVo();
            respPlaceUnitSalePlanVo.setRespPlaceUnitSalePlan(respPlaceUnitSalePlan);
            respPlaceUnitSalePlanVoList.add(respPlaceUnitSalePlanVo);
        }

        Comparator<RespPlaceUnitSalePlanVo> comparator = new Comparator<RespPlaceUnitSalePlanVo>() {
            @Override
            public int compare(RespPlaceUnitSalePlanVo o1, RespPlaceUnitSalePlanVo o2) {
                return o1.getRespPlaceUnitSalePlan().getStartTime()
                        .compareTo(o2.getRespPlaceUnitSalePlan().getStartTime());
            }
        };
        //原始的场馆销售计划与实际销售计划作差集
        List<RespPlaceUnitSalePlanVo> subTractList =
                (List<RespPlaceUnitSalePlanVo>) org.apache.commons.collections
                        .CollectionUtils.subtract(respPlaceUnitSalePlanVoList, dbPlaceUnitSalePlanVoList);

        //把经过以上步骤处理得到的差集与实际销售计划作并集，得到补全后的销售计划
        List<RespPlaceUnitSalePlanVo> unionList =
                (List<RespPlaceUnitSalePlanVo>) org.apache.commons
                        .collections.CollectionUtils.union(subTractList, dbPlaceUnitSalePlanVoList);

        //将补全后的销售计划按起始时间排序
        Collections.sort(unionList, comparator);
        return unionList;
    }


    /**
     * 付款方式规范
     * 1、微信、微信公众号、小程序  adminPayType为：2
     * 2、支付宝、网页版支付宝     adminPayType为：1
     * @param payType
     * @return
     */
    public static Integer getAdminPayType(Integer payType){
        if (payType == PlaceConstants.PAY_WAY_ALIPAY || payType == PlaceConstants.PAY_WAY_FOR_WEB_ALIPAY){
            return PlaceConstants.PAY_WAY_ALIPAY;
        }
        if (payType == PlaceConstants.PAY_WAY_WEICHAT || payType == PlaceConstants.PAY_WAY_WEICHATH5
                || payType == PlaceConstants.PAY_WAY_FOR_WEIXIN_APPLET){
            return PlaceConstants.PAY_WAY_WEICHAT;
        }
        return payType;
    }

    public static List<Integer> getPayType(){
        List<Integer> values = new ArrayList<>();
        values.add(PlaceConstants.PAY_WAY_ALIPAY); // 支付宝
        values.add(PlaceConstants.PAY_WAY_WEICHAT); // 微信支付
        values.add(PlaceConstants.PAY_WAY_WEICHATH5); // 微信公众号支付
        values.add(PlaceConstants.PAY_WAY_FOR_CARD); // 会员卡
        values.add(PlaceConstants.PAY_WAY_FOR_WEB_ALIPAY); // 网页版支付宝
        values.add(PlaceConstants.PAY_WAY_FOR_WEIXIN_APPLET);//微信小程序
        return values;
    }
    
    public static List<Integer> getMapPayId(){
    	 List<Integer> values = new ArrayList<>();
         values.add(PlaceConstants.PAY_WAY_ALIPAY); // 支付宝
         values.add(PlaceConstants.PAY_WAY_WEICHAT); // 微信支付
         values.add(PlaceConstants.PAY_WAY_WEICHATH5); // 微信公众号支付
         values.add(PlaceConstants.PAY_WAY_FOR_WEB_ALIPAY); // 网页版支付宝
         values.add(PlaceConstants.PAY_WAY_FOR_WEIXIN_APPLET);//微信小程序
         return values;
    }

    /**
     * 根据平台确定用户的渠道
     * @param platformType
     * @return
     */
    public static Byte orderChannelByPlatform(Integer platformType){
        /**'订单渠道：1：APP，2：微信公众号，3：H5，4：商家端，5：小程序',**/
        if (platformType == 1 || platformType == 2 ){
            return PlaceConstants.PLACE_APP_PAY_TYPE.byteValue();
        }

        if (platformType == 3 ){
            return PlaceConstants.PLACE_WX_PAY_TYPE.byteValue();
        }

        if (platformType == 4 || platformType == 6 ){
           return PlaceConstants.PLACE_H5_PAY_TYPE.byteValue();
        }

        if (platformType == 5 ){
           return PlaceConstants.PLACE_ADMIN_PAY_TYPE.byteValue();
        }

        if (platformType == 7 ){
            return PlaceConstants.PLACE_WEAPP_PAY_TYPE.byteValue();
        }
        return null;
    }
    /**
     * 根据平台确定sourceType
     * @param platformType
     * @return
     */
    public static Byte orderSourceTypeByPlatform(Integer platformType){
        if (platformType == 1 || platformType == 2 || platformType == 3 || platformType == 4 || platformType == 6 || platformType == 7){
            return PlaceConstants.SOURCE_TYPE_COMMONPAY;
        }
        return null;
    }

    
}
